<?php
/**
 * Created by PhpStorm.
 * User: Sunqj
 * Date: 2016/11/16
 * Time: 下午2:05
 */
namespace app\upload\controller;

use think\Controller;
use think\Log;
use app\common\model\User;

//图片上传
define("H_UPLOAD_IMAGE", "images");
//视频上传
define("H_UPLOAD_VIDEO", "videos");
//文件上传
define("H_UPLOAD_FILE", "files");

class Index extends Controller
{
    public function index()
    {
        //获取参数
        if (request()->isPost())
        {
            User::userFromToken(request()->param('access_token'));
            //单图上传
            if (input("?file.image"))
            {
                $this->exec(H_UPLOAD_IMAGE);
            }
            elseif (input("?file.video"))
            {
                $this->exec(H_UPLOAD_VIDEO);
            }
            elseif (input("?file.file"))
            {
                $this->exec(H_UPLOAD_FILE);
            }
            else
            {
                send_developer_error("无效的上传！");
            }
        }
        else
        {
            send_developer_error("仅接受POST方式文件上传！");
        }
    }

    private function exec($type)
    {
        $file = request()->file();

        //URL列表
        $urlList = array();

        if (is_array($file))
        {
            foreach ($file as $item)
            {
                if (is_array($item))
                {
                    foreach ($item as $ii)
                    {
                        $result = $this->upload($ii, $type);

                        if ($result)
                        {
                            $urlList[] = $result;
                        }
                    }
                }
                else
                {
                    $result = $this->upload($item, $type);

                    if ($result)
                    {
                        $urlList[] = $result;
                    }
                }
            }
        }
        else
        {
            $result = $this->upload($file, $type);

            if ($result)
            {
                $urlList[] = $result;
            }
        }

        if (count($urlList) <= 0)
        {
            send_user_error("上传失败！");
        }

        $response = array(
            "result_code" => "0000",
            "result_info" => "上传成功！",
            "upload_list" => $urlList,
        );
        //判断是否加密
        if (input('?get.no_encrypt'))
        {
            die(json_encode($response, JSON_UNESCAPED_UNICODE));
        }
        else
        {
            die(aes_encrypt(config("aes_encrypt_key"), json_encode($response, JSON_UNESCAPED_UNICODE)));
        }
    }

    /**
     * 上传文件
     *
     * @param object $file 收到的文件对象
     * @return string 服务器端文件URL
     */
    private function upload($file, $type)
    {
        //保存路径
        $savePath = config("upload_path") . '/' . $type;

        //验证条件
        $validation = array();

        //图像
        if ($type == H_UPLOAD_IMAGE)
        {
            $validation["ext"] = "jpg,png,gif,jpeg";
        }

        //视频
        if ($type == H_UPLOAD_VIDEO)
        {
            $validation["ext"] = "mov,mp4,avi,mkv,flv,rm,rmvb";
        }

        // 移动到上传目录下
        $result = $file
            ->rule("upload_save_name")
            ->validate($validation)
            ->move($savePath);

        //上传成功
        if($result)
        {

            return "/uploads/" . $type . '/' . $result->getSaveName();
        }
        else
        {
            Log::write($file->getError());
            // 上传失败获取错误信息
            return null;
        }
    }
}